package com.colin.userservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.colin.annotation.LoginCheck;
import com.colin.bean.User;
import com.colin.core.PathCollect;
import com.colin.response.R;
import com.colin.response.RCM;
import com.colin.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * /user/colin    /person/colin
 */
@RestController
@RequestMapping({"/user", "/person"})
public class UserController {

    @Autowired
    private UserService userService;


    @LoginCheck
    @GetMapping("/{username}")
    public R<Boolean> checkUsernameIsExist(@PathVariable String username) {
        return userService.checkUsernameIsExist(username);
    }

    @LoginCheck
    @PostMapping
    public R<Boolean> doRegister(@RequestBody User user) {
        return userService.doRegister(user);
    }


    //    @LoginCheck
    @PostMapping("/auth")
    public R<User> login(@RequestBody User user) {
        return userService.doLogin(user);
    }

    @LoginCheck
    @GetMapping
    public R<Boolean> isLogin() {
        return StpUtil.isLogin() ? R.ok(true) : R.error(RCM.USER_NOT_LOGIN);
    }

    /**
     * 要求用户必须处在登录状态 才能访问的接口 我们需要去做自己的逻辑处理 验证当前接口这个用户是否有权访问
     *
     * @return
     */
    @LoginCheck
    @DeleteMapping
    public R<Boolean> logout() {
        StpUtil.logout();
        return R.ok(true);
    }

}
